﻿using Plugin.Factory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WJ.Infrastructure.Extension;
using WJ.Infrastructure.Util;

namespace Plugin.Colafile
{
    public class ColafileHandler : IHelper
    {
        public string DiskName
        {
            get { return "可乐网盘"; }
        }


        private List<string> GetConformUrl(string urlList)
        {
            return urlList.GetMatches("colafile.com/(down|file)/[0-9]*");
        }


        public List<ResultMsg> GetDownloadLink(string urlList)
        {

            List<ResultMsg> links = new List<ResultMsg>();

            foreach (var item in GetConformUrl(urlList))
            {
                string id = Regex.Match(item, @"\d+").Value;

                string html = new HttpClient("http://www.colafile.com/ajax.php?action=downaddress&file_id=" + id + "").GetString();

                links.Add(
                    new ResultMsg { OriginalUrl = item, DownloadUrl = Regex.Match(html, "\"http://*.*\"").Value.Replace("\"", "") }

                    );
            }

            return links;
        }


        public bool IsOpen
        {
            get { return true; }
        }


        public Guid EntityId
        {
            get { return Guid.NewGuid(); }
        }
    }
}
